package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.OrderBean;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/4/17.
 * 设置订单信息
 */

public class ActivitySetOrderInfo extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.et_orderid)
    EditText etOrderid;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private OrderBean b;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_set_order_info);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "提交订单号");
    }

    /**
     * orderId,tradeNo(第三方下单号),payAmount(第三方付款金额)
     */
    private void toSave() {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("orderId", b.getDmId() + "");
        map.put("tradeNo", etOrderid.getText().toString().trim());
        map.put("payAmount", etPrice.getText().toString().trim());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, "提交成功");
                    finish();
                } else {
                    ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_GET_THIRD_ORDER, true, map);
    }

    @Override
    public void initData() {
        if(getIntent().getExtras()==null){
            finish();
            ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, "数据有误，请重试");
            return;
        }
        b = (OrderBean) getIntent().getExtras().getSerializable(Constants.KEY_ORDER_BEAN);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_commit)
    public void onViewClicked() {
        if(checkData()){
            toSave();
        }
    }

    private boolean checkData() {
        if(TextUtils.isEmpty(etOrderid.getText().toString().trim())){
            ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, "请输入第三方订单号");
            return false;
        }
        if(TextUtils.isEmpty(etPrice.getText().toString().trim())){
            ToastUtil.getInstance().showToast(ActivitySetOrderInfo.this, "请输入订单金额");
            return false;
        }
        return true;
    }
}
